package cst.a903dss.zyfypt_903dss.Activity;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a903dss.zyfypt_903dss.AmicoolListener;
import cst.a903dss.zyfypt_903dss.Bean.VideoBean;
import cst.a903dss.zyfypt_903dss.R;
import cst.a903dss.zyfypt_903dss.ResModel;
import cst.a903dss.zyfypt_903dss.fragment.ArticleFragment;
import cst.a903dss.zyfypt_903dss.fragment.Fragment2;
import cst.a903dss.zyfypt_903dss.fragment.Fragment3;
import cst.a903dss.zyfypt_903dss.fragment.Fragment4;
import cst.a903dss.zyfypt_903dss.fragment.Fragment5;


public class MainActivity extends AppCompatActivity {
    private LinearLayout  layout1,layout2,layout3,layout4,layout5;//用于界面切换
    private ImageButton ib1,ib2,ib3,ib4,ib5;//用户设置图片
    private ViewPager vp;
    FragmentPagerAdapter fragmentPagerAdapter;
    List<Fragment> listfragment;
    private ResModel model;
    private AmicoolListener<List<VideoBean>> listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
       /* Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        model=new ResModel("gson");
        listener=new AmicoolListener<List<VideoBean>>() {
            @Override
            public void onResponse(List<VideoBean> l) {

                Toast.makeText(MainActivity.this,l.get(0).getName(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {System.out.print(msg);}
        };*/
        init();
        initFragment();
    }
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new ArticleFragment());
        listfragment.add(new Fragment2());
        listfragment.add(new Fragment3());
        listfragment.add(new Fragment4());
        listfragment.add(new Fragment5());
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp=(ViewPager)findViewById(R.id.viewPager);
        vp.setAdapter(fragmentPagerAdapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    View.OnClickListener onClicklistener =new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
            resetImage();
            switch (view.getId())
            {
                case R.id.layout1:
                    Toast.makeText(MainActivity.this,"切换底部栏Article",Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
                    Toast.makeText(MainActivity.this,"切换底部栏课件",Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
                    Toast.makeText(MainActivity.this,"切换底部栏视屏",Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
                    Toast.makeText(MainActivity.this,"切换底部栏设置",Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
                case R.id.layout5:
                    Toast.makeText(MainActivity.this,"切换底部栏我",Toast.LENGTH_SHORT).show();
                    selectTab(4);
            }
        }
    };
    //model.getList("Video",1, listener);
    private  void init(){
        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);
        ib5=(ImageButton)findViewById(R.id.imageButton5);

        layout1=(LinearLayout)findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);

        layout1.setOnClickListener(onClicklistener);
        layout2.setOnClickListener(onClicklistener);
        layout3.setOnClickListener(onClicklistener);
        layout4.setOnClickListener(onClicklistener);
        layout5.setOnClickListener(onClicklistener);
    }
    private void resetImage(){
        ib1.setImageResource(R.drawable.__picker_camera);
        ib2.setImageResource(R.drawable.__picker_ic_broken_image_black_48dp);
        ib3.setImageResource(R.drawable.address_normal);
        ib4.setImageResource(R.drawable.settings_normal);
        ib5.setImageResource(R.drawable.__picker_checkbox_marked);
    }
    private void selectTab(int i){
        switch (i){
            case 0:
                ib1.setImageResource(R.drawable.__picker_camera);
                break;
            case 1:
                ib2.setImageResource(R.drawable.__picker_ic_broken_image_black_48dp);
                break;
            case 2:
                ib3.setImageResource(R.drawable.address_normal);
                break;
            case 3:
                ib4.setImageResource(R.drawable.settings_normal);
                break;
            case 4:
                ib5.setImageResource(R.drawable.__picker_checkbox_marked);

        }
        vp.setCurrentItem(i);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Toast.makeText(this,"you clicked setting",Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
